package com.asia5b.a5mall.middleware.logic.viewmodel.setting

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.utils.ThreadFromUtils
import com.asia5b.a5mall.middleware.utils.glide.ClearUtils
import io.reactivex.Observable
import io.reactivex.Scheduler
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import java.io.File

/**
 * Created by zhangyuncai on 2020/8/19.
 */
class SettingVM : SimpleViewModel() {

    val clearChcheLiveData = MutableLiveData<String>()

    /**
     * @Note   清除缓存
     */
    fun clearChche(context: Context) {
        compositeDisposable.add(Observable.create<String> {
            ClearUtils.getInstance().clearImageDiskCache(context)
            it.onNext("0KB")
        }.compose(ThreadFromUtils.defaultSchedulers())
                .subscribe {
//                    providerView().renderCacheSize(it)
                    clearChcheLiveData.value=it
                })
    }

    fun getCacheSize(context: Context) {
        compositeDisposable.add(Observable.just(1)
                .subscribeOn(Schedulers.io())
                .map {
                    ClearUtils.getFormatSize(ClearUtils.getInstance().getFolderSize(File(context.externalCacheDir!!.path, "Glide")).toDouble())
                }
                .subscribe(Consumer {
                    clearChcheLiveData.value=it
                }, Consumer {
                    it.printStackTrace()
                }))
    }

    val logoutLiveData = MutableLiveData<Boolean>()

    /**
     * @Note   退出登录
     */
    fun logout(uid: String) {
        baseSubscribe(memberApi.logout(uid), SimpleNetParams(), Consumer {
//            providerView().logout()
            logoutLiveData.value=true
        }, Consumer {
//            providerView().logout()
            logoutLiveData.value=true
        })
    }

}